package com.agg.core.utils;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author ptz
 * <p>
 * String 操作共用类
 */

public class Strings {

    /**
     * 指定分割的字符串格式，返回分割后的字符串数组
     *
     * @param str   字符串
     * @param regex 格式
     * @return
     */
    public static List<String> split(String str, String regex) {
        if (Asserts.isNull(str) || Asserts.isNull(regex)) {
            return null;
        }

        return Stream.of(str.split(regex)).collect(Collectors.toList());
    }

    public   static <T> List<T>  splitToNumber(String str, String regex,Class<T> clz)
             {
        if (Asserts.isNull(str) || Asserts.isNull(regex)) {
            return null;
        }
        List<T> list=new ArrayList<>();
        try {
            if(clz!=null){
                Method method = clz.getMethod("valueOf", String.class);
                String[] strs = str.split(regex);
                for(int i=0;i<strs.length;i++){
                    T v=(T)method.invoke(clz,strs[i]);
                    list.add(v);
                }
                return  list;
            }else{
                return null;
            }
        }catch (IllegalAccessException e){
            e.printStackTrace();
        }catch (NoSuchMethodException e){
            e.printStackTrace();
        }catch (InvocationTargetException e){
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 将首字母转大写
     * @param str
     * @return
     */
    public static String toUpperCaseFirstOne(String str) {
        return Character.isUpperCase(str.charAt(0)) ? str : Character.toUpperCase(str.charAt(0)) + str.substring(1);
    }

    /**
     * 字符串转为驼峰格式
     * @param str
     * @return
     */
    public static String toCamelCase(String str,String fmt) {
        if(Asserts.isNullOrEmpty(str)){
            return str;
        }
        String[] array=str.split(fmt);
        if(array.length==1){
            return str;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(array[0]);
        if(array.length>1){
            for(int i=1;i<array.length;i++){
               String item= toUpperCaseFirstOne(array[i]);
               sb.append(item);
            }
        }
        return sb.toString();
    }

    /**
     * 转小写，同时大写字母 按fmt 分割
     * @param str
     * @param fmt
     * @return
     */
    public static String toLowerCase(String str,String fmt) {

        if (str == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        // 前置字符是否大写
        boolean preCharIsUpperCase = true;
        // 当前字符是否大写
        boolean curreCharIsUpperCase = true;
        // 下一字符是否大写
        boolean nexteCharIsUpperCase = true;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (i > 0) {
                preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1));
            } else {
                preCharIsUpperCase = false;
            }
            curreCharIsUpperCase = Character.isUpperCase(c);

            if (i < (str.length() - 1)) {
                nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1));
            }
            if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase) {
                sb.append(fmt);
            } else if ((i != 0 && !preCharIsUpperCase) && curreCharIsUpperCase) {
                sb.append(fmt);
            }
            sb.append(Character.toLowerCase(c));
        }

        return sb.toString();
    }



    public static int indexOf(CharSequence seq, CharSequence searchSeq) {
        if ((seq == null) || (searchSeq == null)) {
            return -1;
        }
        return indexOf(seq, searchSeq, 0);
    }

    public static int indexOf(CharSequence cs, CharSequence searchChar, int start) {
        return cs.toString().indexOf(searchChar.toString(), start);
    }

    public static String join(Object[] array, String separator, int startIndex, int endIndex) {
        if (array == null) {
            return null;
        }
        int noOfItems = endIndex - startIndex;
        if (noOfItems <= 0) {
            return "";
        }
        String separatorCp = "";
        if (Asserts.notNull(separator)) {
            separatorCp = separator;
        }
        StringBuilder buf = new StringBuilder(noOfItems * 16);
        for (int i = startIndex; i < endIndex; i++) {
            if (i > startIndex) {
                buf.append(separatorCp);
            }
            if (array[i] != null) {
                buf.append(array[i]);
            }
        }
        return buf.toString();
    }

    public static String substring(String string, int fromIndex, int toIndex) {
        int len = string.length();
        if (fromIndex < 0) {
            fromIndex = len + fromIndex;
            if (toIndex == 0) {
                toIndex = len;
            }
        }
        if (toIndex < 0) {
            toIndex = len + toIndex;
        }
        if (fromIndex < 0) {
            fromIndex = 0;
        }
        if (toIndex > len) {
            toIndex = len;
        }
        if (fromIndex >= toIndex) {
            return "";
        }
        return string.substring(fromIndex, toIndex);
    }

    public static String join(Object[] array, String separator) {
        if (array == null) {
            return null;
        }
        return join(array, separator, 0, array.length);
    }

    public static String join(List list, String separator) {
        if (list == null) {
            return null;
        }
        Object[] array= list.toArray();
        return join(array, separator, 0, array.length);
    }

    /**
     * 默认按逗号分割
     * @param list
     * @return
     */
    public static String join(List list) {
        if (list == null) {
            return null;
        }
        String separator=",";
        Object[] array= list.toArray();
        return join(array, separator, 0, array.length);
    }

    public static String get32UUID() {
        return UUID.randomUUID().toString().trim().replaceAll("-", "");
    }



    public static void main(String[] args) {



    }
}
